home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Applications / gdbm-1.7.3 / source / dbmopen.c < prev    next >
Text File  |  1994-05-22  |  5KB  |  136 lines

  1. /* dbmopen.c - Open the file for dbm operations. This looks like the
  2.    NDBM interface for dbm use. */
  3.  
  4. /*  This file is part of GDBM, the GNU data base manager, by Philip A. Nelson.
  5.     Copyright (C) 1990, 1991, 1993  Free Software Foundation, Inc.
  6.  
  7.     GDBM is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation; either version 2, or (at your option)
  10.     any later version.
  11.  
  12.     GDBM is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU General Public License
  18.     along with GDBM; see the file COPYING.  If not, write to
  19.     the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21.     You may contact the author by:
  22.        e-mail:  phil@cs.wwu.edu
  23.       us-mail:  Philip A. Nelson
  24.                 Computer Science Department
  25.                 Western Washington University
  26.                 Bellingham, WA 98226
  27.        
  28. *************************************************************************/
  29.  
  30.  
  31. /* AIX demands this be the very first thing in the file. */
  32. #if !defined(__GNUC__) && defined(_AIX)
  33.  #pragma alloca
  34. #endif
  35.  
  36. /* include system configuration before all else. */
  37. #include "autoconf.h"
  38.  
  39. #include "gdbmdefs.h"
  40. #include "gdbmerrno.h"
  41. #include "extern.h"
  42.  
  43. /* Initialize ndbm system.  FILE is a pointer to the file name.  In
  44.    standard dbm, the database is found in files called FILE.pag and
  45.    FILE.dir.  To make gdbm compatable with dbm using the dbminit call,
  46.    the same file names are used.  Specifically, dbminit will use the file
  47.    name FILE.pag in its call to gdbm open.  If the file (FILE.pag) has a
  48.    size of zero bytes, a file initialization procedure is performed,
  49.    setting up the initial structure in the file.  Any error detected will
  50.    cause a return value of -1.  No errors cause the return value of 0.
  51.    NOTE: file.dir will be ignored and will always have a size of zero.
  52.    FLAGS and MODE are the same as the open (2) call.  This call will
  53.    look at the FLAGS and decide what call to make to gdbm_open.  For
  54.    FLAGS == O_RDONLY, it will be a GDBM_READER, if FLAGS == O_RDWR|O_CREAT,
  55.    it will be a GDBM_WRCREAT (creater and writer) and if the FLAGS == O_RDWR,
  56.    it will be a GDBM_WRITER and if FLAGS contain O_TRUNC then it will be
  57.    a GDBM_NEWDB.  All other values of FLAGS in the flags are
  58.    ignored. */
  59.  
  60. gdbm_file_info *
  61. dbm_open (file, flags, mode)
  62.      char *file;
  63.      int flags;
  64.      int mode;
  65. {
  66.   char* pag_file;        /* Used to construct "file.pag". */
  67.   char* dir_file;        /* Used to construct "file.dir". */
  68.   struct stat dir_stat;        /* Stat information for "file.dir". */
  69.   gdbm_file_info *temp_dbf;  /* Temporary file pointer storage. */
  70.  
  71.  
  72.   /* Prepare the correct names of "file.pag" and "file.dir". */
  73.   pag_file = (char *) alloca (strlen (file)+5);
  74.   dir_file = (char *) alloca (strlen (file)+5);
  75.  
  76.   strcpy (pag_file, file);
  77.   strcat (pag_file, ".pag");
  78.   strcpy (dir_file, file);
  79.   strcat (dir_file, ".dir");
  80.   
  81.  
  82.   /* Call the actual routine, saving the pointer to the file information. */
  83.   flags &= O_RDONLY | O_RDWR | O_CREAT | O_TRUNC;
  84.   if (flags == O_RDONLY)
  85.     {
  86.       temp_dbf = gdbm_open (pag_file, 0, GDBM_READER, 0, NULL);
  87.     }
  88.   else if (flags == (O_RDWR | O_CREAT))
  89.     {
  90.       temp_dbf = gdbm_open (pag_file, 0, GDBM_WRCREAT, mode, NULL);
  91.     }
  92.   else if ( (flags & O_TRUNC) == O_TRUNC)
  93.     {
  94.       temp_dbf = gdbm_open (pag_file, 0, GDBM_NEWDB, mode, NULL);
  95.     }
  96.   else
  97.     {
  98.       temp_dbf = gdbm_open (pag_file, 0, GDBM_WRITER, 0, NULL);
  99.     }
  100.  
  101.   /* Did we successfully open the file? */
  102.   if (temp_dbf == NULL)
  103.     {
  104.       gdbm_errno = GDBM_FILE_OPEN_ERROR;
  105.       return NULL;
  106.     }
  107.     
  108. #ifndef THINK_C
  109.   /* If the database is new, link "file.dir" to "file.pag". This is done
  110.      so the time stamp on both files is the same. */
  111.   if (stat (dir_file, &dir_stat) == 0)
  112.     {
  113.       if (dir_stat.st_size == 0)
  114.     if (unlink (dir_file) != 0 || link (pag_file, dir_file) != 0)
  115.       {
  116.         gdbm_errno = GDBM_FILE_OPEN_ERROR;
  117.         gdbm_close (temp_dbf);
  118.         return NULL;
  119.       }
  120.     }
  121.   else
  122.     {
  123.       /* Since we can't stat it, we assume it is not there and try
  124.          to link the dir_file to the pag_file. */
  125.       if (link (pag_file, dir_file) != 0)
  126.     {
  127.       gdbm_errno = GDBM_FILE_OPEN_ERROR;
  128.       gdbm_close (temp_dbf);
  129.       return NULL;
  130.     }
  131.     }
  132. #endif
  133.         
  134.   return temp_dbf;
  135. }
  136.